//
//  StatusCell.swift
//  MyBlog
//
//  Created by lvtienan on 15/8/6.
//  Copyright © 2015年 ltn. All rights reserved.
//

import UIKit

/// 控件间距
let statusCellControlMargin: CGFloat = 8.0

// 微博 Cell 的枚举
enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    // 静态函数
    static func cellID(status: Status) -> String {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatusCell: UITableViewCell {

    // 微博数据模型
    var status: Status? {
        didSet {
            topView.status = status
            contentLabel.text = status?.text
            
            // 给 pictureView 的 statuses 赋值
            pictureView.statuses = status
            pictureWithCon?.constant = pictureView.bounds.size.width
            pictureHeightCon?.constant = pictureView.bounds.size.height
            pictureTopCons?.constant = (pictureView.bounds.height == 0) ? 0 : statusCellControlMargin
        }
    }
    
    // 图片宽度和图片高度约束
    var pictureWithCon: NSLayoutConstraint?
    var pictureHeightCon: NSLayoutConstraint?
    //  顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    // 计算行高
    func rowHeight(status: Status) -> CGFloat {
        self.status = status
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    // MARK: - 界面设置
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
 
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置界面
    func setupUI() {
        
        // 1. 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 2. 设置布局
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 53))
        // 内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellControlMargin))

        // 底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
        // 底部约束
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
    }
    
    // MARK: - 懒加载控件
    // 顶部视图
    private lazy var topView:StatusTopView = StatusTopView()
    // 文字内容
    lazy var contentLabel:UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        // 设置文本折行的宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellControlMargin
        
        return label
        }()
    // 配图
    lazy var pictureView: StatusPictureView = StatusPictureView()
    // 底部视图
    lazy var bottomView:StatusBottomView = StatusBottomView()

}
